package com.andyou.rest.web;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.ws.rs.Consumes;
import javax.ws.rs.FormParam;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.andyou.common.mapper.JsonMapper;
import com.andyou.common.persistence.Page;
import com.andyou.common.utils.SpringContextHolder;
import com.andyou.common.utils.StringUtils;
import com.andyou.common.ws.ReturnResult;
import com.andyou.rest.entity.AuthEnum;
import com.andyou.rest.entity.ImplLsData;
import com.andyou.rest.entity.RequestHeader;
import com.andyou.rest.entity.ResponseBody;
import com.andyou.rest.service.ImplLsDataService;

@Path("/sfimpllsdata")
@Component
public class ImplLsDataController {

	/**
	 * 日志对象
	 */
	protected Logger logger = LoggerFactory.getLogger(ImplLsDataController.class);
	@Autowired
	private ImplLsDataService implLsDataService;
	
	/**
	 * 获取大屏展示数据统计结果
	 * 
	 * @param requestHeader 
	 * 			验证信息
	 * @param pageSize
	 *            每页显示条数
	 * @param pageIndex
	 *            第几页
	 * @param where
	 *        查询条件
	 *  @param orderby
	 *        排序字段
	 * @return 获取大屏展示数据统计结果列表（json）
	 * @author lgx Date:2016年5月5日；
	 */
	@POST
	@Path("/getStatisticalList")
	@Produces(MediaType.APPLICATION_JSON)
	@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
	public ResponseBody<ReturnResult> getStatisticalList(@FormParam("requestHeader") String requestHeader,@FormParam("pageSize") int pageSize,@FormParam("pageIndex") int pageIndex,
			@FormParam("where") String where,@FormParam("orderby") String orderby) 
	{
		ResponseBody<ReturnResult> body = new ResponseBody<ReturnResult>();
		try
		{
			RequestHeader header  = (RequestHeader)JsonMapper.fromJsonString(requestHeader,RequestHeader.class);
			if(header.getSignCode()!=null)
			{
				ImplLsData implLsData = new ImplLsData();
				if(StringUtils.isNotBlank(where.trim()))
				{
					implLsData = (ImplLsData)JsonMapper.fromJsonString(where.trim(),ImplLsData.class);
				}
				
				Page<ImplLsData> page = new Page<ImplLsData>();
				page.setPageSize(pageSize);
				page.setPageNo(pageIndex);
				if(StringUtils.isNoneEmpty(orderby))
					page.setOrderBy(orderby);
				else
					page.setOrderBy("");
				implLsData.setPage(page);
				ReturnResult returnResult = new ReturnResult();
				returnResult.setList(implLsDataService.findStatisticalList(implLsData));
				Map<String,Object> dataPagination = new HashMap<String, Object>();
				dataPagination.put("totalRow", implLsData.getPage().getCount());
				dataPagination.put("pageSize", pageSize);
				dataPagination.put("pageIndex", implLsData.getPage().getPageNo());
				long getrow = pageSize*pageIndex-implLsData.getPage().getCount();
				if(getrow>=pageSize)
				{
					returnResult.setList(null);
				}
				
				returnResult.setPage(dataPagination);
				body.setResultCode(String.valueOf(AuthEnum.success.ordinal()));
				body.setResultMsg(AuthEnum.success.toString());
				body.setReturnResult(returnResult);
			}
			else
			{
				body.setResultCode(String.valueOf(AuthEnum.failloginexception.ordinal()));
				body.setResultMsg(AuthEnum.failloginexception.toString());
			}
		}
		catch(Exception e)
		{
			logger.debug(e.getMessage());
			body.setResultCode(String.valueOf(AuthEnum.faildateerror.ordinal()));
			body.setResultMsg(AuthEnum.faildateerror.toString());
		}
		return body;
	}

	/**
	 * *每天跑报表数据
	 * 
	 * @return
	 */
	public void dayEndImplSrv(){
		logger.info("******************日终大屏接口统计开始" + new Date()+ "******************");
		//implLsDataService = SpringContextHolder.getBean(ImplLsDataService.class);
		//this.implLsDataService.dayEnd(new Date());
		logger.info("******************日终大屏接口统计结束" + new Date()+ "******************");
	}
}
